#include <smurfSubControllItem.h>
#include <netAddressChecker.h>
#include <postManager.h>

namespace View {

  bool SmurfAttackButton::OnClicked()
  {
    //    std::cout<<"SmurfAttackButton::OnClicked\n";
    if(true == buttonType && !(smurfControll->CheckInputData()) )
      return false;

    if(true == buttonType)
      smurfControll->SendAllAddress();

    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_ATTACK_STAT);
    poster->SetDoubleData(Model::SMURF_ATTACK);
    poster->SetBoolData(buttonType);
    poster->SendBroadcast();
    return true;
  }

  SmurfSubControllItem::SmurfSubControllItem(GooCanvasItem *parent)
    :SubControllItem(parent),
     targetIPEntry(gtk_entry_new()),
     broadcastAddrEntry(gtk_entry_new())
  {
    GdkPixbuf *bg,*mg,*fg;
    std::string bgPath,mgPath,fgPath;
    ViewConf *conf = ViewConf::GetViewConf();
    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "smurfSubItemBg.png";
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    goo_canvas_image_new(root,bg,conf->GetDoubleData("SubControllItemBgX"),
                         conf->GetDoubleData("SubControllItemBgY"),NULL);
    g_object_unref(bg);


    double smurfEntryX(conf->GetDoubleData("smurfEntryX")),
      smurfEntryY(conf->GetDoubleData("smurfEntryY")),
      smurfEntryLength(conf->GetDoubleData("smurfEntryLength")),
      smurfEntryHigh(conf->GetDoubleData("smurfEntryHigh")),
      smurfMessageX(conf->GetDoubleData("smurfMessageX")),
      smurfMessageY(conf->GetDoubleData("smurfMessageY")),
      smurfLabelL(conf->GetDoubleData("smurfLabelL")),
      smurfMessageL(conf->GetDoubleData("smurfMessageL"));
    goo_canvas_widget_new(root,targetIPEntry,smurfEntryX,
			  smurfEntryY,smurfEntryLength,
			  smurfEntryHigh,NULL);
    goo_canvas_widget_new(root,broadcastAddrEntry,smurfEntryX,
                          smurfEntryY + smurfEntryHigh*4,
                          smurfEntryLength,smurfEntryHigh,
                          NULL);
    std::string font("Sans 17");
    std::string color("#0A1CC0");
    targetIPMsg = goo_canvas_text_new(root,conf->GetStringData("smurfTargetIPMsg").c_str(),
				      smurfEntryX,smurfEntryY - smurfEntryHigh,
				      smurfLabelL,GTK_ANCHOR_NW,
				      "font", font.c_str(),"fill_color",
				      color.c_str(),NULL);
    broadcastAddrMsg  = goo_canvas_text_new(root,conf->GetStringData("smurfBroadcastAddrMsg").c_str(),
					    smurfEntryX,smurfEntryY + smurfEntryHigh*3,
					    smurfLabelL,GTK_ANCHOR_NW,
					    "font", font.c_str(),"fill_color",
					    color.c_str(),NULL);
    color = "#000000";
    smurfDetailMsg  = goo_canvas_text_new(root,conf->GetStringData("smurfDetailMsg").c_str(),
					  smurfMessageX,smurfMessageY,
					  smurfMessageL,GTK_ANCHOR_NW,
					  "font", font.c_str(),"fill_color",
					  color.c_str(),NULL);
    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "starAttackBg.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackMg.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackFg.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    startAttack = new SmurfAttackButton(root,conf->GetDoubleData("arpAttackButtonX"),
				       conf->GetDoubleData("arpAttackButtonY"),
				       bg,mg,fg,true,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);
    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "pauseAttackBgpng.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackMgpng.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackFgpng.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    stopAttack = new SmurfAttackButton(root,conf->GetDoubleData("arpAttackButtonX"),
				       conf->GetDoubleData("arpAttackButtonY"),
				       bg,mg,fg,false,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);
    stopAttack->Hide();

  }

  SmurfSubControllItem::~SmurfSubControllItem()
  {

  }

  int SmurfSubControllItem::OnReceive(Controller::Broadcast *broadcast)
  {
    using namespace Controller;
    BroadcastType type = broadcast->GetBroadcastType();
    switch(type)
      {
      case BROADCAST_TYPE_ATTACK_STAT:{
        if(true == broadcast->GetBoolData() && 
	   Model::SMURF_ATTACK == broadcast->GetDoubleData() ){
          startAttack->Hide();
          stopAttack->Show();
        }else if(false == broadcast->GetBoolData() &&
		 Model::SMURF_ATTACK == broadcast->GetDoubleData() ){
          stopAttack->Hide();
          startAttack->Show();
        }
      }break;
      case BROADCAST_TYPE_CHANGE_LANGUAGE:{
	ViewConf *conf = ViewConf::GetViewConf();
	g_object_set(targetIPMsg,"text",conf->GetStringData("smurfTargetIPMsg").c_str(),NULL);
	g_object_set(broadcastAddrMsg,"text",conf->GetStringData("smurfBroadcastAddrMsg").c_str(),NULL);
	g_object_set(smurfDetailMsg,"text",conf->GetStringData("smurfDetailMsg").c_str(),NULL);
      }break;
      default:return 1;
      }
    return 0;
  }

  bool SmurfSubControllItem::SendAllAddress()
  {
    std::string targetIP(gtk_entry_get_text((GtkEntry*)targetIPEntry)),
      broadcastAddr(gtk_entry_get_text((GtkEntry*)broadcastAddrEntry)),
      allAddress(targetIP + "#" + broadcastAddr);
    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_SMURF_ALL_ADDRESS);
    poster->SetStringData(allAddress);
    poster->SendBroadcast();
    return true;
  }

  bool SmurfSubControllItem::CheckInputData()
  {
    std::string targetIP(gtk_entry_get_text((GtkEntry*)targetIPEntry)),
      broadcastAddr(gtk_entry_get_text((GtkEntry*)broadcastAddrEntry));
    NetAddressChecker checker;
    ViewConf *conf = ViewConf::GetViewConf();
    MessageDialog *dialog = MessageDialog::GetMessageDialog();
    dialog->SetTitle(conf->GetStringData("smurfDialogTitle"));
    if(!checker.CheckIP(targetIP))
      dialog->SetText(conf->GetStringData("smurfTargetIPWrong"));
    else if(!checker.CheckIP(broadcastAddr))
      dialog->SetText(conf->GetStringData("smurfBroadcastAddrWrong"));
    else
      return true;
    dialog->Show();
    return false;
  }


}
